class CoordinateMapper {
  constructor() {
    this.sceneBounds = {
      ueMinX: 0,
      ueMaxX: 1000,
      ueMinY: 0,
      ueMaxY: 1000
    }
    this.containerRect = null
  }

  // 初始化场景参数
  initSceneParams({ ueMinX, ueMaxX, ueMinY, ueMaxY }, containerElement) {
    this.sceneBounds = { ueMinX, ueMaxX, ueMinY, ueMaxY }
    this.containerRect = containerElement.getBoundingClientRect()
  }

  // 网页坐标转UE场景坐标
  screenToUE(x, y) {
    if (!this.containerRect) {
      throw new Error('CoordinateMapper not initialized')
    }
    
    const relativeX = (x - this.containerRect.left) / this.containerRect.width
    const relativeY = (y - this.containerRect.top) / this.containerRect.height

    return {
      ueX: this.sceneBounds.ueMinX + relativeX * (this.sceneBounds.ueMaxX - this.sceneBounds.ueMinX),
      ueY: this.sceneBounds.ueMinY + relativeY * (this.sceneBounds.ueMaxY - this.sceneBounds.ueMinY)
    }
  }

  // UE场景坐标转网页坐标
  ueToScreen(ueX, ueY) {
    if (!this.containerRect) {
      throw new Error('CoordinateMapper not initialized')
    }

    const relativeX = (ueX - this.sceneBounds.ueMinX) / (this.sceneBounds.ueMaxX - this.sceneBounds.ueMinX)
    const relativeY = (ueY - this.sceneBounds.ueMinY) / (this.sceneBounds.ueMaxY - this.sceneBounds.ueMinY)

    return {
      x: this.containerRect.left + relativeX * this.containerRect.width,
      y: this.containerRect.top + relativeY * this.containerRect.height
    }
  }
}

export const coordinateMapper = new CoordinateMapper()